﻿package {
	import flash.display.Sprite;
	import com.kerry.page.PageSwitch;
	import flash.events.MouseEvent;
	
	/**
	 * com.kerry.page.PageSwitch 类的演示文件
	 * @author PhoenixKerry（http://blog.sina.com.cn/yyy98）
	 */
	public class Main extends Sprite {
		private var pageSwitch:PageSwitch;
		
		public function Main() {
			var friendList:Array = [];
			for (var i:uint = 0; i < 50; i++ ) {
				var user:Object = new Object();
				user.name = "No." + i;
				user.age = i;
				friendList.push(user);
			}
			
			pageSwitch = new PageSwitch(friendList, 12, showCallBack);
			prev_btn.addEventListener(MouseEvent.CLICK, prevPageHandler);
			next_btn.addEventListener(MouseEvent.CLICK, nextPageHandler);
			
			pageSwitch.currentPage = 1;
			page_txt.text = pageSwitch.currentPage + " / " + pageSwitch.maxPage;
		}
		
		private function nextPageHandler(e:MouseEvent):void {
			pageSwitch.currentPage++;
		}
		
		private function prevPageHandler(e:MouseEvent):void {
			pageSwitch.currentPage--;
		}
		
		/**
		 * 在 UserBox 中显示当前页的内容
		 * @param	viewList: 由 PageSwitch 返回当前页的数据列表
		 */
		private function showCallBack(viewList:Array):void {
			var ubox:UserBox;
			
			// 隐藏舞台上所有的 UserBox 对象
			for (var k:uint = 1; k <= 12 ; k++ ) {
				ubox = getChildByName("ubox_" + k) as UserBox;
				ubox.visible = false;
			}
			
			// 在 UserBox 中显示 viewList 中的用户数据
			for (var i:uint = 0; i < viewList.length; i++ ) {
				ubox = getChildByName("ubox_" + (i + 1)) as UserBox;
				ubox.visible = true;
				
				var user:Object = viewList[i] as Object;
				ubox.name_txt.text = user.name;
				ubox.age_txt.text = String(user.age);
			}
			
			page_txt.text = pageSwitch.currentPage + " / " + pageSwitch.maxPage;
		}
		
	}
}